import store from '../store';

// oldBirthday life childHeight bmr sas mbti baozhiqi wordCount subGroup
// jobInterest jiuxing roi birth oil babyWeight geShui turnTable mockCat
// tuixiu
const modelType = "life";

let appMap = {
	"mockCat": {
		"douyin": {
			appId: "tt271551a8b829f94501",
			adVideoId: "",
			adBannerId: "",
		},
		"wechat": {
			appId: "",
			adVideoId: "",
			adBannerId: "",
		},
		"kuaishou": {
			appId: "",
			adVideoId: "",
			adBannerId: "",
		},
		"baidu": {
			appId: "",
			adVideoId: "",
			adBannerId: "",
		},
	},
	"wordCount": {
		"douyin": {
			appId: "tt271551a8b829f94501",
			adVideoId: "",
			adBannerId: "",
		},
		"wechat": {
			appId: "",
			adVideoId: "",
			adBannerId: "",
		},
		"kuaishou": {
			appId: "",
			adVideoId: "",
			adBannerId: "",
		},
		"baidu": {
			appId: "",
			adVideoId: "",
			adBannerId: "",
		},
	},
	"subGroup": {
		"douyin": {
			appId: "tt271551a8b829f94501",
			adVideoId: "",
			adBannerId: "",
		},
		"wechat": {
			appId: "",
			adVideoId: "",
			adBannerId: "",
		},
		"kuaishou": {
			appId: "",
			adVideoId: "",
			adBannerId: "",
		},
		"baidu": {
			appId: "",
			adVideoId: "",
			adBannerId: "",
		},
	},
	"turnTable": {
		"douyin": {
			appId: "tt271551a8b829f94501",
			adVideoId: "",
			adBannerId: "",
		},
		"wechat": {
			appId: "",
			adVideoId: "",
			adBannerId: "",
		},
		"kuaishou": {
			appId: "",
			adVideoId: "",
			adBannerId: "",
		},
		"baidu": {
			appId: "",
			adVideoId: "",
			adBannerId: "",
		},
	},
	"fuli": {
		"douyin": {
			appId: "tt271551a8b829f94501",
			adVideoId: "8vhbukktpvh4zinuk1",
			adBannerId: "4x9x4si430y1d4se6y",
		},
		"wechat": {
			appId: "",
			adVideoId: "",
			adBannerId: "",
		},
		"kuaishou": {
			appId: "",
			adVideoId: "",
			adBannerId: "",
		},
		"baidu": {
			appId: "",
			adVideoId: "",
			adBannerId: "",
		},
	},
	"baozhiqi": {
		"douyin": {
			appId: "ttfa8a8c376581150b01",
			adVideoId: "ac308qkf5rqa70z849",
			adBannerId: "k5v567pzpnpnqb4swd",
		},
		"wechat": {
			appId: "",
			adVideoId: "",
			adBannerId: "",
		},
		"kuaishou": {
			appId: "",
			adVideoId: "",
			adBannerId: "",
		},
		"baidu": {
			appId: "",
			adVideoId: "",
			adBannerId: "",
		},
	},
	"tuixiu": {
		"douyin": {
			appId: "tt5ada2c67c5d1b57d01",
			adVideoId: "fpxmd9m6fmi1mxxzh9",
			adBannerId: "uuwan2ev4zlt9wlw9k",
		},
		"wechat": {
			appId: "",
			adVideoId: "",
			adBannerId: "",
		},
		"kuaishou": {
			appId: "",
			adVideoId: "",
			adBannerId: "",
		},
		"baidu": {
			appId: "",
			adVideoId: "",
			adBannerId: "",
		},
	},
	"geShui": {
		"douyin": {
			appId: "tt74d7026aebdd2e2501",
			adVideoId: "knjfrkpfrku19bebq0",
			adBannerId: "vmzn2tko1ezepuiwhy",
		},
		"wechat": {
			appId: "",
			adVideoId: "",
			adBannerId: "",
		},
		"kuaishou": {
			appId: "",
			adVideoId: "",
			adBannerId: "",
		},
		"baidu": {
			appId: "",
			adVideoId: "",
			adBannerId: "",
		},
	},
	"babyWeight": {
		"douyin": {
			appId: "tt7ad00c6de5db954901",
			adVideoId: "gxmmgp3bc4aityx1a1",
			adBannerId: "g9wz1ce8rcbdcdd9ep",
		},
		"wechat": {
			appId: "",
			adVideoId: "",
			adBannerId: "",
		},
		"kuaishou": {
			appId: "",
			adVideoId: "",
			adBannerId: "",
		},
		"baidu": {
			appId: "",
			adVideoId: "",
			adBannerId: "",
		},
	},
	"oil": {
		"douyin": {
			appId: "tta1cdd7703ec7d6ac01",
			adVideoId: "937jnkimctfurys0yx",
			adBannerId: "vkq80b45liaghcvu4e",
		},
		"wechat": {
			appId: "",
			adVideoId: "",
			adBannerId: "",
		},
		"kuaishou": {
			appId: "",
			adVideoId: "",
			adBannerId: "",
		},
		"baidu": {
			appId: "",
			adVideoId: "",
			adBannerId: "",
		},
	},
	"birth": {
		"douyin": {
			appId: "tt90a5e6180fafe71501",
			adVideoId: "xyh4f52fonstjf77iq",
			adBannerId: "raxymr8ogzaywfh63f",
		},
		"wechat": {
			appId: "",
			adVideoId: "",
			adBannerId: "",
		},
		"kuaishou": {
			appId: "",
			adVideoId: "",
			adBannerId: "",
		},
		"baidu": {
			appId: "",
			adVideoId: "",
			adBannerId: "",
		},
	},
	"roi": {
		"douyin": {
			appId: "tt8137f78a1e3df65801",
			adVideoId: "slmnw3hn6mjznspa4l",
			adBannerId: "tc3ah9rgfder607lzn",
		},
		"wechat": {
			appId: "",
			adVideoId: "",
			adBannerId: "",
		},
		"kuaishou": {
			appId: "",
			adVideoId: "",
			adBannerId: "",
		},
		"baidu": {
			appId: "",
			adVideoId: "",
			adBannerId: "",
		},
	},
	"jobInterest": {
		"douyin": {
			appId: "tt228e5433bdb2e25f01",
			adVideoId: "7yw0wh6apyf917bm12",
			adBannerId: "05fxof9z9jc2xdtrh6",
		},
		"wechat": {
			appId: "",
			adVideoId: "",
			adBannerId: "",
		},
		"kuaishou": {
			appId: "",
			adVideoId: "",
			adBannerId: "",
		},
		"baidu": {
			appId: "",
			adVideoId: "",
			adBannerId: "",
		},
	},
	"jiuxing": {
		"douyin": {
			appId: "tt304446f9af78ad5e01",
			adVideoId: "jbgfv9xwbfndkpes46",
			adBannerId: "h42av51ujiicc65ino",
		},
		"wechat": {
			appId: "",
			adVideoId: "",
			adBannerId: "",
		},
		"kuaishou": {
			appId: "",
			adVideoId: "",
			adBannerId: "",
		},
		"baidu": {
			appId: "",
			adVideoId: "",
			adBannerId: "",
		},
	},
	"mbti": {
		"douyin": {
			appId: "tt97eae15708d6518201",
			adVideoId: "lbh0eotnynyue51e0a",
			adBannerId: "qbtqf1py34e62b1o9z",
		},
		"wechat": {
			appId: "",
			adVideoId: "",
			adBannerId: "",
		},
		"kuaishou": {
			appId: "",
			adVideoId: "",
			adBannerId: "",
		},
		"baidu": {
			appId: "",
			adVideoId: "",
			adBannerId: "",
		},
	},
	"sas": {
		"douyin": {
			appId: "ttd71fa1a24823094c01",
			adVideoId: "miehx4o3e5orztyhe8",
			adBannerId: "vknrhr8dyaxvsew357",
		},
		"wechat": {
			appId: "",
			adVideoId: "",
			adBannerId: "",
		},
		"kuaishou": {
			appId: "",
			adVideoId: "",
			adBannerId: "",
		},
		"baidu": {
			appId: "",
			adVideoId: "",
			adBannerId: "",
		},
	},
	"oldBirthday": {
		"douyin": {
			appId: "ttc5dbb3665ad1b0b001",
			adVideoId: "uwa0r95t3bsmj3ji7p",
			adBannerId: "c9ee57qc3kulczkjla",
		},
		"wechat": {
			appId: "",
			adVideoId: "",
			adBannerId: "",
		},
		"kuaishou": {
			appId: "",
			adVideoId: "",
			adBannerId: "",
		},
		"baidu": {
			appId: "",
			adVideoId: "",
			adBannerId: "",
		},
	},
	"life": {
		"douyin": {
			appId: "tt02e7745a9ded760001",
			adVideoId: "57d5rj3iofo4zaff5h",
			adBannerId: "pss3ivaiikgy2e6fym",
		},
		"wechat": {
			appId: "wx819d637ea4dff2c2",
			adVideoId: "",
			adBannerId: "",
		},
		"kuaishou": {
			appId: "",
			adVideoId: "",
			adBannerId: "",
		},
		"baidu": {
			appId: "",
			adVideoId: "",
			adBannerId: "",
		},
	},
	"childHeight": {
		"douyin": {
			appId: "ttf50b83b66a007e2f01",
			adVideoId: "czia71s4p1dvc3dol6",
			adBannerId: "qbnptlipu7nmuu7dim",
		},
		"wechat": {
			appId: "wx9639ea1a928e5ef1",
			adVideoId: "",
			adBannerId: "",
		},
		"kuaishou": {
			appId: "",
			adVideoId: "",
			adBannerId: "",
		},
		"baidu": {
			appId: "",
			adVideoId: "",
			adBannerId: "",
		},
	},
	"bmr": {
		"douyin": {
			appId: "tt6adf1c62f5d4854701",
			adVideoId: "mxlyy6w9wyjpadktxd",
			adBannerId: "dkjktrr9r0ho43oqnh",
		},
		"wechat": {
			appId: "",
			adVideoId: "",
			adBannerId: "",
		},
		"kuaishou": {
			appId: "",
			adVideoId: "",
			adBannerId: "",
		},
		"baidu": {
			appId: "",
			adVideoId: "",
			adBannerId: "",
		},
	},
}

let curAppInfo = appMap[modelType];

console.log("curAppInfo", curAppInfo);

// 应用appId和密钥
const wxAI = "wx646bd90a57c75c02";
const wxAS1 = "1e94f682d2d";
const wxAS2 = "89f64361795";
const wxAS3 = "8b3b559c82";
const wxAS = wxAS1 + wxAS2 + wxAS3;
const dyAI = "tt304446f9af78ad5e01";
const dyAS = "068112444a3964eb3fc9f53466126efc00f7a16b";
const ksAI = "ks684828621360804118";
const ksAS = "rQWn8T-oC4n4JbroVcxFIg";
const zhipuApiKey = "b1afd145e99cf0d3a43ca3aa71fa288d.9n4veEpK2u3JafgN";

// 接口地址
const apiUrl = "https://mini02.viyacm.online?ac=";
const apiUrl2 = "https://www.viyacm.com/wp-admin/admin-ajax.php?action=";
const chatUrl = "https://open.bigmodel.cn/api/paas/v4/chat/completions";
const preUrl = "https://www.viyacm.com/";
const fileUrl = "https://cdn.viyacm.com/mini_";
const sinaFileUrl = "https://tva1.sinaimg.cn/large/";
const orgFileUrl = "https://cdn.viyacm.com/";
2
// 平台广告ID
const toutiaoAdUnitId = "ig6up90wqb6i2of09a";
const weixinAdUnitId = "ig6up90wqb6i2of09a";

// 是否正式版
const isRelease = true;

let isLoading = false;

// 用户信息
const userInfo = {
	id: null,
	nickName: "",
	thumb: null,
	city: "",
	country: "",
	gender: 0,
	unionId: null
}

// 获取平台标识
function getProvider() {
	let provider = null;
	// #ifdef H5
	provider = "h5";
	// #endif
	// #ifdef MP-WEIXIN
	provider = "weixin";
	// #endif
	// #ifdef MP-TOUTIAO
	let platfrom = uni.getSystemInfoSync();
	if (platfrom.hostName == "Douyin") {
		provider = "douyin";
	} else if (platfrom.hostName == "Toutiao") {
		provider = "toutiao";
	} else {
		provider = platfrom.hostName;
	}
	// #endif
	// #ifdef MP-KUAISHOU
	provider = "kuaishou";
	// #endif
	// #ifdef MP-BAIDU
	provider = "baidu";
	// #endif
	return provider;
}

// 复制文本
function copyText(text) {
	uni.setClipboardData({
		data: text
	})
}

// 跳转链接
function toPage(url) {
	uni.navigateTo({
		url: url
	})
}

// 跳转链接
function toast(str) {
	uni.showToast({
		title: str,
		icon: "none",
		duration: 2000
	})
}

function filterHtml(html) {
	let str = "";
	str = html.replace(/(<([^>]+)>)/ig, "");
	return str;
}

function showLoading() {
	store.state.isLoading = true;
}

function hideLoading() {
	store.state.isLoading = false;
}

// 获取路由信息
function getRoutes() {
	let routes = getCurrentPages();
	let curRoute = routes[routes.length - 1].route;
	return curRoute;
}

function random(min, max) {
	return Math.floor(Math.random() * (max - min)) + min;
}

// 是否网页
function isH5() {
	if (platform == "H5") {
		return true;
	} else {
		return false;
	}
}

// 通用接口
function api(options) {
	// uni.showLoading({
	// 	title: "加载中..."
	// });
	return new Promise((resolve, reject) => {
		let url = "";
		if (options.isThirtPart) {
			url = options.url;
		} else {
			url = apiUrl + options.url;
		}
		uni.request({
			url: url,
			data: options.data || {},
			method: options.method || "GET",
			header: {
				"Content-type": options.contentType == "json" ? "application/json" : "application/x-www-form-urlencoded"
			},
			dataType: "json",
			success: res => {
				// uni.hideLsoading();
				resolve(res);
			},
			fail: err => {
				// uni.hideLoading();
				reject(err);
			}
		})
	})
}

// 通用接口
function chatApi(options) {
	return new Promise((resolve, reject) => {
		uni.request({
			url: chatUrl,
			data: {
				model: "GLM-4-Flash",
				messages: [
					{ role: "system", content: [{ type: "text", text: options.data }] },
					{
						role: "assistant",
						content: [{ type: "text", text: "收到" }],
					},
				],
			},
			method: "POST",
			header: {
				"Content-type": "application/json",
				'Authorization': 'Bearer ' + zhipuApiKey,
			},
			dataType: "json",
			success: res => {
				if (res.data) {
					if (res.data.choices.length > 0) {
						resolve(res.data.choices[0].message.content);
					}
				}
			},
			fail: err => {
				reject(err);
			}
		})
	})
}

// 获取Token
function getTouTiaoToken(callBack) {
	let postData = {
		appid: dyAI,
		secret: dyAS,
		grant_type: "client_credential"
	}
	api({
		url: "https://developer.toutiao.com/api/apps/v2/token",
		method: "POST",
		isThirtPart: true,
		contentType: "json",
		data: postData
	}).then(res => {
		console.log("res0", res);
		if (res.statusCode == 200) {
			if (callBack) {
				callBack(res.data);
			}
		}
	})
}

function verifyTouTiaoContent(str, token, callBack) {
	let postData = {
		str: str,
		toutiaoToken: token
	}
	api({
		url: "verifyContent",
		method: "POST",
		data: postData
	}).then(res2 => {
		let data = res2.data;
		let flag = false;
		console.log("res2", res2);
		if (data.data) {
			for (let i in data.data) {
				console.log(data.data[i]);
				for (let j in data.data[i].predicts) {
					console.log("predicts", data.data[i].predicts[j]);
					if (data.data[i].predicts[j].prob == 1) {
						flag = true;
					}
				}
			}
		}
		if (flag) {
			toast("输入内容存在风险，请重新输入");
		} else {
			callBack();
		}
	})
}

// 获取用户信息
function getUserInfo(info, callBack) {
	uni.getUserInfo({
		provider: platform,
		success: res2 => {
			console.log("获取用户信息成功", res2);
			userInfo.nickName = res2.userInfo.nickName;
			userInfo.city = res2.userInfo.city;
			userInfo.country = res2.userInfo.country;
			userInfo.thumb = res2.userInfo.avatarUrl;
			userInfo.gender = res2.userInfo.gender;
			switch (platform) {
				case "weixin": {
					uni.request({
						url: "https://api.weixin.qq.com/sns/jscode2session",
						method: "POST",
						header: {
							"content-type": "application/x-www-form-urlencoded"
						},
						data: {
							appid: wxAI,
							secret: wxAS,
							js_code: info.code,
							grant_type: "authorization_code"
						},
						success: res3 => {
							console.log("获取unionId成功", res3);
							userInfo.unionId = res3.data.unionid;
							updateUserInfo(userInfo, platform);
							callBack(res3);
						}
					});
					break;
				}
				case "toutiao": {
					uni.request({
						url: "https://developer.toutiao.com/api/apps/v2/jscode2session",
						method: "POST",
						data: {
							provider: info.platform,
							appid: dyAI,
							secret: dyAS,
							code: info.code,
							anonymous_code: ""
						},
						success: res3 => {
							console.log("获取unionId成功", res3);
							userInfo.unionId = res3.data.data.unionid;
							updateUserInfo(userInfo, platform);
							callBack(res3);
						}
					});
					break;
				}
				case "kuaishou": {
					uni.request({
						url: "https://open.kuaishou.com/oauth2/mp/code2session",
						method: "POST",
						header: {
							"content-type": "application/x-www-form-urlencoded"
						},
						data: {
							app_id: ksAI,
							app_secret: ksAS,
							js_code: info.code
						},
						success: res3 => {
							console.log("获取unionId成功", res3);
							callBack(res3);
						}
					});
					break;
				}
				default: {
					console.log("不支持该平台");
				}
			}
		}
	});
}

// 登录
function login(callBack) {
	if (platform && platform !== "") {
		uni.login({
			provider: platform,
			success: res => {
				console.log("登录成功", res);
				getUserInfo(res, res2 => {
					callBack(res2);
				});
			}
		});
	} else {
		console.log("获取服务商失败");
	}
}

// 清空用户数据
function clearUserInfo() {
	userInfo = {
		id: null,
		nickName: "",
		thumb: null,
		city: "",
		country: "",
		gender: 0,
		unionId: null
	}
}

// 更新用户信息
function updateUserInfo(userInfo, platform) {
	console.log(userInfo);
	if (userInfo.unionId) {
		switch (platform) {
			case "weixin": {
				let userData = {
					nickName: userInfo.nickName,
					thumb: userInfo.thumb,
					provider: userInfo.provider,
					city: userInfo.city || "",
					country: userInfo.country || "",
					gender: userInfo.gender || "",
					unionId: userInfo.unionId
				}
				uni.setStorageSync("userInfo", userData);
				break;
			}
			case "toutiao": {
				let userData = {
					nickName: userInfo.nickName,
					thumb: userInfo.thumb,
					provider: "",
					city: "",
					country: "",
					gender: userInfo.gender,
					unionId: userInfo.unionId
				}
				uni.setStorageSync("userInfo", userData);
				break;
			}
		}
	} else {
		console.log("获取unionId失败");
	}
}

module.exports = {
	modelType,
	curAppInfo,
	// 通用常量
	wxAI,
	wxAS,
	dyAI,
	dyAS,
	ksAI,
	ksAS,
	apiUrl,
	apiUrl2,
	preUrl,
	fileUrl,
	orgFileUrl,
	sinaFileUrl,
	isRelease,
	isLoading,
	userInfo,
	// 通用函数
	api,
	chatApi,
	random,
	toast,
	copyText,
	isH5,
	toPage,
	filterHtml,
	login,
	getProvider,
	getTouTiaoToken,
	getRoutes,
	verifyTouTiaoContent,
	clearUserInfo,
	showLoading,
	hideLoading
}